home *** CD-ROM | disk | FTP | other *** search
/ Nebula 1 / Nebula One.iso / Utilities / Workspace / Briefcase / Source / Utility.m < prev   
Text File  |  1995-06-12  |  3KB  |  102 lines

  1. #import "Global.h"
  2. #import <appkit/PrintInfo.h>
  3.  
  4. #define CHUNK 127
  5.  
  6. extern void getAppDirectory (char *appDirectory)
  7. {
  8.     FILE *process;
  9.     char command[256];
  10.     char *suffix;
  11.  
  12.     strcpy (appDirectory,NXArgv[0]);
  13.     if (appDirectory[0] == '/') {         /* if absolute path */
  14.         if (suffix = rindex(appDirectory,'/')) 
  15.             *suffix  = '\0';             /* remove executable name */
  16.     } else {
  17.         sprintf(command,"which '%s'\n",NXArgv[0]);
  18.         process=popen(command,"r");
  19.         fscanf(process,"%s",appDirectory);
  20.         pclose(process);
  21.         if (suffix = rindex(appDirectory,'/')) 
  22.             *suffix  = '\0';             /* remove executable name */
  23.         chdir(appDirectory);
  24.         getwd(appDirectory);
  25.     }  
  26. #ifdef DEBUG
  27.     printf("%s\n",appDirectory);
  28. #endif
  29. }
  30.  
  31. char *stripnl(char *s)
  32. {
  33.     char *p;
  34.     for (p=s;*p;p++) if (*p == '\n' || *p == '\r') *p = '\0';
  35.     return s;
  36. }
  37.  
  38. char *execstr(char *s)
  39. /* Executes the passed string and returns a string value in that pointer */
  40. /* Stolen from Opener 3.0 */
  41. {
  42.     FILE *f = popen(s,"r");
  43.     char *p = s;
  44.     *s = '\0';
  45.     if (f){
  46.         while (fgets(p,256,f))
  47.             stripnl(p), p += strlen(p);
  48.         pclose(f);
  49.     }
  50.     return s;
  51. }
  52.  
  53. /* Functions for creating and managing a dynamically-allocated fileList */
  54. /* - really just a list of strings.  They seem to be somewhat useful.   */
  55.  
  56. char **addFile(const char *file, char **list, int count, NXZone *zone)
  57. /* Adds the specified filename to the list of filenames.  It allocates 
  58.  * more memory in chunks as needed.
  59.  */
  60. {    
  61.     if (!list) list = (char **)NXZoneMalloc(zone,CHUNK*sizeof(char *));
  62.     list[count] = (char *)NXZoneMalloc(zone,(strlen(file)+1)*sizeof(char));
  63.     strcpy(list[count], file);
  64.     count++;
  65.     if (!(count% CHUNK)) {
  66.         list = (char **)NXZoneRealloc(zone,list,(((count/CHUNK)+1)*CHUNK)*sizeof(char *));
  67.     }
  68.     list[count] = NULL;
  69.     return list;
  70. }
  71.  
  72. void freeList(char **list)
  73. /* Frees the array of filenames */
  74.  {
  75.     char **strings;
  76.  
  77.     if (list) {
  78.         strings = list;
  79.         while (*strings) NX_FREE(*strings++);
  80.         NX_FREE(list);
  81.     }
  82. }
  83.  
  84.  
  85. NXRect *calcFrame(id printInfo, NXRect *viewRect)
  86. /*
  87.  * Calculates the size of the page the user has chosen minus its margins.
  88.  */
  89. {
  90.     float lm, rm, bm, tm;
  91.     const NXRect *paperRect;
  92.  
  93.     viewRect->origin.x = viewRect->origin.y = 0.0;
  94.     paperRect = [printInfo paperRect];
  95.     [printInfo getMarginLeft:&lm right:&rm top:&tm bottom:&bm];
  96.     viewRect->size = paperRect->size;
  97.     viewRect->size.width -= lm + rm;
  98.     viewRect->size.height -= tm + bm;
  99.  
  100.     return viewRect;
  101. }
  102.